<script type="text/javascript">
	function showNew(){
		$('.addnew').slideToggle();	
	}
</script>

<?php
	
	require_once(dirname(__FILE__) . "/../core/safeParam.php");
	
	$action   = $_GET['action'];
	$username = $_SESSION['username'];
	
	switch ($action) {
		default:
			if (!isset($_GET['lid'])) {
				$results = $database->select("SELECT * FROM ads WHERE rid = " . ARID . "  group by name order by name");
      		
      		echo '
      		<div style="margin-left:20px;">
					<h2>Agregar Banner</h2>
				</div>

				<br />				
				
				<div style="margin-left:20px;">
					<a href="index.php?edit=ads&action=add">
      				<img src="../admin/images/icons/add.png" border="0">&nbsp;&nbsp;Agregar banner
      			</a>
				</div>
				
				<br />
      		
      		<table "width="95" cellspacing="2" cellpadding="2" class="table" style="margin:auto;">
      			<tr>
      				<th>ID</th>
      				<th>Nombre</th>
      				<th>Funciones</th>
      			</tr>';
      			
      		if ($results != false) {
      			for ($i = 0; $i < count($results); $i++) {
      				echo '
      				<tr>
                  	<td style="padding:5px;border-bottom:1px dashed #CCC">' . $results[$i]['id'] . '  </td>
                  	
                  	<td style="padding:5px;border-bottom:1px dashed #CCC">
                  		<a href="index.php?edit=ads&lid=' . $results[$i]['name'] . '">' . $results[$i]['name'] . ' </a>
                  	</td>
                  	
                  	<td style="padding:5px;border-bottom:1px dashed #CCC">
                  		<a href="index.php?edit=ads&action=edit&lid=' . $results[$i]['name'] . '">
                  			<img src="../admin/images/icons/application_edit.png"  border="0">&nbsp;Editar
                  		</a>     

								<br />                  		
                  		
                  		<a href="../functions/ads.php?function=delete&id=' . $results[$i]['id'] . '">
                  			<img src="../admin/images/icons/delete.png"  border="0" style="margin-left:10px">&nbsp;Eliminar
                  		</a> 
                  	</td>
                  	

                	</tr>';
        			}
      		} echo '</table>';
    		} else {
    			$results = $database->select("SELECT * FROM ads WHERE    name = '" . $_GET['lid'] . "' 	 and rid = " . ARID . " ");
      		
      		echo '
      		<table cellspacing="0" cellpadding="2"  class="table">
      			<tr>
        				<td>Tamaño</td>
        				
        				<td>
        					' . $results[0]['name'] . ' -  (' . $results[0]['width'] . ' Width x ' . $results[0]['height'] . ' Height)
        				</td>
        			</tr>
        			
        			<tr>
						<td colspan="2">
							Coloque el código siguiente en la plantilla: 
							<span style="font-size:.8em">
								' . htmlentities('<script type="text/javascript" src="ads.php?function=serve&id=' . $results[0]['name'] . '"></script>') . '
							</span>
						</td>
        			</tr>
				</table>
				
				<h2>Nuevo banner</h2>
				<form action="../functions/ads.php?function=add&return=' . $_GET['lid'] . '" method="post" enctype="multipart/form-data" name="form" >
            	<input type="hidden" name="rid" value="' . ARID . '">		
        			<input type="hidden" name="width" size="55" value="' . $results[0]['width'] . '">
        			<input type="hidden" name="name" size="55" value="' . $results[0]['name'] . '">
        			<input type="hidden" name="height" size="55" value="' . $results[0]['height'] . '">
        			
        			<table cellspacing="0" cellpadding="5" cellspacing="4" class="table_form" >
          			<tr>
            			<td>URL:</td>
            			<td>
            				<input type="text" name="url" size="55">
								<select name="target">
            					<option value="_blank">Abrir en nueva ventana</option>
            					<option value="">Abrir en la misma ventana</option>
            				</select>
            			</td>
          			</tr>
          
          			<tr>
            			<td>Imagen</td>
            			<td>
              				<input type="file" name="image" size="55" >
              				<br>
            			</td>
          			</tr>
          
          			<tr>
            			<td colspan="2">
            				<div align="center">
									<input type="submit" value="Agregar" style="paddin:10px;">
            				</div>
            			</td>
          			</tr>
        			</table>
        		</form>
        		<h2>Banners Existentes</h2>';
      		
      		$results_ads = $database->select("SELECT * FROM ads WHERE    name = '" . $results[0]['name'] . "'  and rid = " . ARID . " ");
      		
      		if ($results_ads != false) {
      			for ($i = 0; $i < count($results_ads); $i++) {
      				if ($results_ads[$i]['target'] == '') {
      					$selected  = 'selected="selected"';
            			$selected2 = '';
          			} else {
            			$selected  = '';
            			$selected2 = 'selected="selected"';
          			}          			
          		
          		echo '
          		<div style="border:1px solid #CCC; background-color:#f1f1f1;padding:10px;margin:10px">
               	<form action="../functions/ads.php?function=edit&return=' . $_GET['lid'] . '" method="post"  enctype="multipart/form-data" name="form_edit' . $i . '">
               		<input type="hidden" name="rid" value="' . ARID . '">		
               		<input type="hidden" name="id" value="' . $results_ads[$i]['id'] . '">
               		<input type="hidden" name="height" value="' . $results_ads[$i]['height'] . '">
               		<input type="hidden" name="width" value="' . $results_ads[$i]['width'] . '">
               		
               		<table cellspacing="0" cellpadding="2" border="0"  style="text-align:left"  class="table">
               			<tr>
            					<td>Impressiones</td>
            					<td>' . $results_ads[$i]['imp'] . '</td>
            				</tr>
            				
            				<tr>
            					<td>Clicks:</td>
            					<td>' . $results_ads[$i]['clicks'] . '</td>
            				</tr>
            				
            				<tr>
            					<td>Agregar URL:</td>
            					<td>
            						<input type="text" name="url" size="55" value="' . $results_ads[$i]['url'] . '">
            						<select name="target">
            							<option ' . $selected2 . ' value="_blank">Abrir en nueva ventana</option>
            							<option value="" ' . $selected . '>Abrir en la misma ventana</option>
            						</select>
            					</td>
            				</tr>
            				
            				<tr>
            					<td colspan="2">
            						<div>
            							<a href="' . $results_ads[$i]['url'] . '" target="_blank">
            								<img src="../UserFiles/Image/product_photos/' . $results_ads[$i]['image'] . '" border="0">
            							</a>
            						</div>
            					</td>
            				</tr>

            				<tr>
            					<td colspan="2">
            						<a href="../functions/ads.php?function=delete&id=' . $results_ads[$i]['id'] . '&return=' . $_GET['lid'] . '">
            							<img src="../admin/images/icons/delete.png">&nbsp;&nbsp;Eliminar
            						</a>
            					</td>
            				</tr>
            				
								<tr>            				            					
            					<td colspan="2">
            						<input type="submit" value="Guardar" style="padding:10px;">  
            					</td>
            				</tr>
            			</table>
            		</div>
            	</form>';
        		} 
      	} 
    	}
    	break;
  		
  		case "add":
    		echo '
    		<form action="../functions/ads.php?function=add" method="post" enctype="multipart/form-data" name="form" >
        		<input type="hidden" name="rid" value="' . ARID . '">		
        		<table cellspacing="2" cellpadding="2" class="table_form">
        			<tr>
        				<td>
        					Nombre
        					<br>
        					<em style="font-weight:normal;">
        						Sin espacios! esto es sólo para identificar la publicidad
        					</em>
        				</td>
        				
        				<td>
        					<input type="text" name="name" size="55" value="' . $results[0]['name'] . '">
        				</td>
        			</tr>
               
               <tr>
        				<td>Ancho</td>
        				<td>
        					<input type="text" name="width" size="15" value="' . $results[0]['name'] . '">
        				</td>
        			</tr>
               
               <tr>
        				<td>Alto</td>
        				<td>
        					<input type="text" name="height" size="15" value="' . $results[0]['name'] . '">
        				</td>
        			</tr>
        
               <tr>
        				<td>URL</td>
        				<td>
        					<input type="text" name="url" size="20">
        					<select name="target">
        						<option value="_blank">Abrir en nueva ventana</option>
        						<option value="">Abrir en la misma ventana</option>
        					</select>
        				</td>
        			</tr>
					
					<tr>
        				<td>Imagen</td>
        				<td>
        					<input type="file" name="image"><br>
	        			</td>
        			</tr>
        
        			<tr>
        				<td colspan="2">
        					<div align="center">
        						<input type="submit" value="Guardar" style="padding:10px;">
        					</div>
        				</td>
        			</tr>
        		</table>
        	</form>';
    break;
} //$action
?>
